""" 
DS1302时钟 读写

接线
    CE -- 23 使能
    IO -- 19 数据
    CK -- 18 时钟
"""

from machine import Pin
import time
import utime
import DS1302

CE_PIN = 23
IO_PIN = 19
CLK_PIN = 18
ds1302 = DS1302.DS1302(clk=Pin(CLK_PIN), dio=Pin(IO_PIN), cs=Pin(CE_PIN))


def read_ds1302_datetime():
    # 读取时间
    date_time = ds1302.DateTime()
    buf = "%d-%d-%d %d:%d:%d" % (
        date_time[0],
        date_time[1],
        date_time[2],
        # date_time[3], weekday 不做处理
        date_time[4],
        date_time[5],
        date_time[6],
    )
    return buf


def read_sys_datetime():
    date_time = utime.localtime()
    buf = "%d-%d-%d %d:%d:%d" % (
        date_time[0],
        date_time[1],
        date_time[2],
        date_time[3],
        date_time[4],
        date_time[5],
    )
    return buf


if __name__ == "__main__":
    # 1.读ds1302时间
    print("sys datetime: {}".format(read_sys_datetime()))
    print("ds1302 datetime: %s" % (read_ds1302_datetime()))

    # 2.写ds1302时间
    ds1302.DateTime([2022, 8, 10, 2, 11, 3, 58])
    while True:
        print("ds1302 datetime: %s" % (read_ds1302_datetime()))
        time.sleep(2)
